﻿using System;
using UnityEngine;
using Verse;
using Verse.Sound;
using RimWorld;

namespace AlphaBehavioursAndEvents
{
    public class Web_ProjectileExtension : Web_Projectile
    {
        protected override void Impact(Thing hitThing, bool blockedByShield)
        {
            base.Impact(hitThing);

            if (hitThing != null)
            {
                DamageDef damageDef = this.def.projectile.damageDef;
                float amount = (float)base.DamageAmount;
                float armorPenetration = base.ArmorPenetration;
                float y = this.ExactRotation.eulerAngles.y;
                Thing launcher = this.launcher;
                ThingDef equipmentDef = this.equipmentDef;
                Pawn hitPawn = hitThing as Pawn;
                DamageInfo dinfo = new DamageInfo(damageDef, amount, armorPenetration, y, launcher, null, null, DamageInfo.SourceCategory.ThingOrUnknown, this.intendedTarget.Thing);


                if (this.def.defName == "AA_MotherWeb")//普通网
                {
                    //IntVec3 a = new IntVec3();
                    //a.GetFirstPawn()

                    var target = damageDef.Worker.ExplosionCellsToHit(hitThing.Position, hitThing.Map, 3);
                    foreach (IntVec3 vec3 in target)
                    {

                        Pawn p = vec3.GetFirstPawn(hitThing.Map);
                        if (p != null)
                        {
                            p.TakeDamage(dinfo);
                        }
                    }

                    Mote mote = (Mote)ThingMaker.MakeThing(WebDefOf.WebEffect);
                    mote.exactPosition = hitThing.DrawPos;
                    GenSpawn.Spawn(mote, hitThing.Position, hitThing.Map);


                }

                if (this.def.defName == "AA_FrostMotherWeb")//霜网
                {
                    DamageInfo dinfo2 = new DamageInfo(DamageDefOf.Frostbite, amount / 2, armorPenetration, y, launcher, null, null, DamageInfo.SourceCategory.ThingOrUnknown, this.intendedTarget.Thing);
                    hitThing.TakeDamage(dinfo2);

                    if (hitPawn != null)
                    {
                        if (hitPawn.health.hediffSet.GetFirstHediffOfDef(HediffDefOf.Hypothermia) is Hediff hypo && hypo != null)//低温
                        {
                            hypo.Severity += 0.1f;
                        }
                        else
                        {
                            hitPawn.health.AddHediff(HediffDefOf.Hypothermia);
                            hitPawn.health.hediffSet.GetFirstHediffOfDef(HediffDefOf.Hypothermia).Severity = 0.1f;
                        }
                    }
                }
                if (this.def.defName == "AA_FireMotherWeb")//火网
                {
                    DamageInfo dinfo2 = new DamageInfo(DamageDefOf.Burn, amount / 2, armorPenetration, y, launcher, null, null, DamageInfo.SourceCategory.ThingOrUnknown, this.intendedTarget.Thing);
                    hitThing.TakeDamage(dinfo2);
                    if (hitPawn != null)
                    {
                        if (hitPawn.health.hediffSet.GetFirstHediffOfDef(HediffDefOf.Heatstroke) is Hediff hypo && hypo != null)//低温
                        {
                            hypo.Severity += 0.1f;
                        }
                        else
                        {
                            hitPawn.health.AddHediff(HediffDefOf.Heatstroke);
                            hitPawn.health.hediffSet.GetFirstHediffOfDef(HediffDefOf.Heatstroke).Severity = 0.1f;
                        }
                    }
                }
                if (this.def.defName == "AA_AcidicMotherWeb")//酸性网
                {

                }
                if (this.def.defName == "AA_ExplodingMotherWeb")//爆炸网
                {

                }
            }
        }
    }
}

